import {IconifyInfo} from '@iconify/types'
import {FetchUtil} from '@xtail/req'

/**
 * 获取 Iconify 图标集信息对象
 * Iconify 文档：https://iconify.design/docs/api/collections.html
 *
 * @param {string[]} prefixes 图标集前缀列表，用于指定要获取的图标集
 * @returns {Promise<Record<string, IconifyInfo>>} Iconify 图标集信息对象，每个图标集的名称为 key，图标集信息为 value
 */
export const getIconSets = async (prefixes: string[]): Promise<Record<string, IconifyInfo>> => {
  let url = 'https://api.iconify.design/collections'
  if (prefixes.length > 0) url += `?prefixes=${prefixes.join(',')}&pretty=1`
  else url += `?pretty=1`
  return await FetchUtil.req({url})
}

export type GetIconifyIconSetsType = typeof getIconSets

/**
 * 获取 Iconify 图标集信息对象
 * Iconify 文档：https://iconify.design/docs/api/collections.html
 *
 * @param {string} prefix 图标集前缀，用于指定要获取的图标集
 * @returns {Promise<Record<string, IconifyInfo>>} Iconify 图标集信息对象，图标集的名称为 key，图标集信息为 value
 */
export const getIconSet = async (prefix: string): Promise<Record<string, IconifyInfo>> => {
  return await getIconSets([prefix])
}

export type GetIconifyIconSetType = typeof getIconSet
